//
//  HD_HSP_Search_ViewController.m
//  HDLiaoNingMeseum
//
//  Created by HDHaoShaoPeng on 2017/8/7.
//  Copyright © 2017年 HDNiuKuiming. All rights reserved.
//

#import "HD_HSP_Search_ViewController.h"
#import "HD_HSP_Exhibit_Model.h"
#import "HD_LY_DownloadManager.h"
#import "HD_HSP_Player_ViewController.h"

@interface HD_HSP_Search_ViewController ()<UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate>
@property (strong, nonatomic) IBOutlet UITableView *myTableview;
@property (weak, nonatomic) IBOutlet UISearchBar *mySearchBar;

@property (strong, nonatomic) NSArray *myDataArray;
@property (strong, nonatomic)HD_HSP_Exhibit_Model  *detailItem;
@property (strong, nonatomic) HD_LY_DownloadManager *myDownloader;
@property (strong, nonatomic) UILabel *noResourceLabel;

@end

static HDDeclare *declare;
@implementation HD_HSP_Search_ViewController
{
    NSArray *_allArray;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    declare = [HDDeclare sharedDeclare];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    _allArray = [declare giveMeExhibitData];
    
    self.myDataArray = _allArray;
    
    _myTableview.dataSource = self;
    _myTableview.delegate = self;
    
    _mySearchBar.placeholder = declare.autoGuideSearch;
    _mySearchBar.delegate = self;
    _mySearchBar.keyboardType = UIKeyboardTypeNumberPad;
    
//    [_mySearchBar setBackgroundImage:[UIImage imageNamed:@"navi_trans"]];
//    self.navigationItem.titleView = _mySearchBar;
    
//    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    rightButton.frame = CGRectMake(0.0, self.view.frame.size.width-60, 30, 30);
//    [rightButton setImage:[UIImage imageNamed:@"resource_search_n"] forState:UIControlStateNormal];
//    [rightButton addTarget:self action:@selector(mySearchAction:) forControlEvents:UIControlEventTouchUpInside];
//    UIBarButtonItem *rightBarButtonItem =[[UIBarButtonItem alloc]initWithCustomView:rightButton];
//    [self.navigationItem setRightBarButtonItem:rightBarButtonItem];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.detailItem  = nil;
    [_myTableview reloadData];
}

- (void)mySearchAction:(id)sender
{
    if (_mySearchBar != nil && [_mySearchBar isFirstResponder])
    {
        [_mySearchBar resignFirstResponder];
    }
    else if (_mySearchBar != nil && ![_mySearchBar isFirstResponder])
    {
        [_mySearchBar becomeFirstResponder];
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [self.myDownloader cleanDownloder];
    self.myDownloader = nil;
}

#pragma mark - 点击方法

- (IBAction)backClicked:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)searchClicked:(id)sender {
    [self mySearchAction:nil];
}


#pragma mark - ------- TableView Delegate Methods -------

- (void)tipLabelRelease
{
    if (_noResourceLabel != nil)
    {
        [_noResourceLabel removeFromSuperview];
        self.noResourceLabel = nil;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 75;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    [self tipLabelRelease];
    
    if (_myDataArray.count == 0 )
    {
        [tableView setHidden:YES];
        _noResourceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 120, self.view.frame.size.width, 40)];
        _noResourceLabel.backgroundColor = [UIColor clearColor];
        _noResourceLabel.text = declare.InvalidInput;
        _noResourceLabel.textAlignment = NSTextAlignmentCenter;
        _noResourceLabel.font = [UIFont systemFontOfSize:22.0];
        [self.view addSubview:_noResourceLabel];
        return 0;
    }
    
    [tableView setHidden:NO];
    return _myDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"mySearchCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    HD_HSP_Exhibit_Model *myResource = _myDataArray[indexPath.row];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@%@", declare.autoGuideNumber, myResource.FileNum];
    cell.textLabel.textColor = Text_Color;
    cell.textLabel.font = [UIFont fontWithName:@"FZQKBYSJW--GB1-0" size:20.0];
    cell.detailTextLabel.font = [UIFont fontWithName:@"FZQKBYSJW--GB1-0" size:15.0];
    
    
    
    cell.textLabel.text = myResource.FileName;
    
    NSString *imgPath = [NSString stringWithFormat:@"%@/%@/%@/%@_icon.png", declare.catch_path, declare.language.uppercaseString, myResource.FileNum,myResource.FileNum];
//    if (myResource..integerValue == 10)
//    {
//        imgPath = @"NO";
//    }
    UIImage *cellImage = [UIImage imageWithContentsOfFile:imgPath];
    if (cellImage == nil)
        cellImage = [UIImage imageNamed:@"list_icon_default"];
    cell.imageView.image = cellImage;
    
    
    UIImageView *rightImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cell_next"]];
    cell.accessoryView = rightImage;
    
    UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cell_selected"]];
    cell.selectedBackgroundView = image;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    HD_HSP_Exhibit_Model *model = self.myDataArray[indexPath.row];
    
    UIStoryboard *board = [UIStoryboard storyboardWithName:@"HD_HSP_Data" bundle:nil];
    HD_HSP_Player_ViewController *vc = [board instantiateViewControllerWithIdentifier:@"HD_HSP_Player_ViewController"];
    vc.detailItem = model;
    [self.navigationController pushViewController:vc animated:YES];
}


//- (BOOL) shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
//{
//    //    int myIndex = [_myTableview indexPathForSelectedRow].row;
//    //    self.detailItem = _myDataArray[myIndex];
//    NSString *mp3Path = [NSString stringWithFormat:@"%@.mp3", self.detailItem.identify];
//    NSString *resourcePath = [NSString stringWithFormat:@"%@/Resource/%@/%@",[declare applicationLibraryDirectory], [declare.language uppercaseString], mp3Path];
//    if ([[NSFileManager defaultManager]fileExistsAtPath:resourcePath])
//    {
//        return YES;
//    }
//    else
//    {
//        [self downloadAction];
//        return NO;
//    }
//}


#pragma mark
#pragma mark -------- SearchDisplay Delegate --------

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    return YES;
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if (searchText.length == 0)
    {
        _myDataArray = _allArray;
    }
    else
    {
        NSPredicate *resultPredicate = [NSPredicate                                      predicateWithFormat:@"FileNum contains[cd] %@",                                     searchBar.text];
        
        self.myDataArray = [_allArray filteredArrayUsingPredicate:resultPredicate];
        
    }
    [_myTableview reloadData];
}


#pragma mark
#pragma mark ------- Dealloc Methods --------

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)dealloc
{
    self.myDataArray = nil;
    self.detailItem  = nil;
    [self tipLabelRelease];
    [_myTableview removeFromSuperview];
    self.myTableview = nil;
}

@end
